package golf.stockdata.framework.spring;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.AbstractBeanDefinition;

public class StockDataBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    private Map abilityInitProps = new HashMap();
    private Map filterInitProps = new HashMap();

    /**
     * Creates a new instance of BocBeanFactoryPostProcessor
     */
    public StockDataBeanFactoryPostProcessor() {
    }

    /**
     * Entry point for Spring.
     *
     * @param configurableListableBeanFactory DOCUMENT ME!
     *
     * @throws org.springframework.beans.BeansException DOCUMENT ME!
     */
    public void postProcessBeanFactory(
        ConfigurableListableBeanFactory configurableListableBeanFactory)
        throws org.springframework.beans.BeansException {
        String[] names = configurableListableBeanFactory.getBeanDefinitionNames();

        for (int ii = 0; ii < names.length; ii++) {
            BeanDefinition bd = configurableListableBeanFactory.getBeanDefinition(names[ii]);
            
            MutablePropertyValues mpv = bd.getPropertyValues();

            // are there properties for this bean?
            // is one named initializeAbility
            if (mpv.contains("initializeAbility")) {
                PropertyValue pv = mpv.getPropertyValue("initializeAbility");
                AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;

                // hold on to the value of this property, register it by bean name.
                //jiaq
                Properties pros = (Properties)pv.getValue();
                Properties newPros = convertProperties(pros);
                abilityInitProps.put(names[ii], newPros);
                
                //abilityInitProps.put(names[ii], pv.getValue());
                mpv.removePropertyValue(pv);
            }

            if (mpv.contains("initializeFilter")) {
                PropertyValue pv = mpv.getPropertyValue("initializeFilter");
                AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;

                // hold on to the value of this property, register it by bean name.
                filterInitProps.put(names[ii], pv.getValue());
                mpv.removePropertyValue(pv);
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param name DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public Properties getAbilityInitProps(String name) {
        return (Properties) abilityInitProps.get(name);
    }

    /**
     * DOCUMENT ME!
     *
     * @param name DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public Properties getFilterInitProps(String name) {
        return (Properties) filterInitProps.get(name);
    }
    
    private Properties convertProperties(Properties pros)
    {
        Enumeration keys = pros.keys();
        Properties newPros = new Properties();
        TypedStringValue key;
        TypedStringValue value;
        for(; keys.hasMoreElements(); newPros.put(key.getValue(), value.getValue()))
        {
            key = (TypedStringValue)keys.nextElement();
            value = (TypedStringValue)pros.get(key);
        }

        return newPros;
    }

}
